package com.shixun.dds_11.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shixun.dds_11.context.BaseContext;
import com.shixun.dds_11.entity.*;
import com.shixun.dds_11.mapper.DdsCartMapper;
import com.shixun.dds_11.mapper.DdsGoodsMapper;
import com.shixun.dds_11.mapper.DdsOrderDetailMapper;
import com.shixun.dds_11.mapper.DdsOrderMapper;
import com.shixun.dds_11.result.PageResult;
import com.shixun.dds_11.result.Result;
import com.shixun.dds_11.service.IDdsCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shixun.dds_11.vo.DdsCartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YSP
 * @since 2024-07-05
 */
@Service
@Slf4j
public class DdsCartServiceImpl extends ServiceImpl<DdsCartMapper, DdsCart> implements IDdsCartService {

    @Autowired
    DdsCartMapper ddsCartMapper;

    @Autowired
    DdsGoodsMapper ddsGoodsMapper;

    @Autowired
    DdsOrderDetailMapper ddsOrderDetailMapper;

    @Autowired
    DdsOrderMapper ddsOrderMapper;

    @Override
    public Result insert(DdsGoods ddsGoods) {

        Integer id = BaseContext.getCurrentId().intValue();
        DdsCart ddsCart=new DdsCart();
        ddsCart.setUserId(id);
        ddsCart.setSubtotal(ddsGoods.getPrice()*(double)ddsGoods.getCount());
        log.info("{}",ddsCart.getSubtotal());
        ddsCart.setCount(ddsGoods.getCount());
        ddsCart.setGoodsId(ddsGoods.getId());
        ddsCartMapper.insert(ddsCart);
        return Result.success();

    }

    @Override
    public PageResult getList(Integer id) {

        QueryWrapper<DdsCart> queryWrapper=new QueryWrapper<DdsCart>()
                .eq("user_id",id);
        List<DdsCart> ddsCarts = ddsCartMapper.selectList(queryWrapper);
        int total=ddsCarts.size();
        List<DdsCartVO> ddsCartVOList=new ArrayList<>();
        if(total!=0)
        for (DdsCart ddsCart : ddsCarts) {

            DdsGoods ddsGoods = ddsGoodsMapper.selectById(ddsCart.getGoodsId());
            if(ddsGoods!=null){
                DdsCartVO ddsCartVO = new DdsCartVO();
                ddsCartVO.setCart_id(ddsCart.getId());
                ddsCartVO.setName(ddsGoods.getName());
                ddsCartVO.setCount(ddsCart.getCount());
                ddsCartVO.setPrice(ddsGoods.getPrice());
                ddsCartVO.setUnit(ddsGoods.getUnit());
                ddsCartVO.setPic(ddsGoods.getPic());
                ddsCartVO.setSubtotal(ddsCart.getSubtotal());
                ddsCartVOList.add(ddsCartVO);
            }

        }

        return new PageResult(total,ddsCartVOList);
    }

    @Override
    public Result change(Integer id,Integer count) {


        DdsCart ddsCart = ddsCartMapper.selectById(id);
        ddsCart.setSubtotal((ddsCart.getSubtotal()*(double)count)/(double)(ddsCart.getCount()));
        ddsCart.setCount(count);

        ddsCartMapper.updateById(ddsCart);
        return Result.success();
    }

    @Override
    @Transactional
    public Integer toPay(ArrayList<Integer> ids,int userId) {

        LocalDateTime dateTime = LocalDateTime.now();
        String NO=dateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+userId;
        DdsOrder ddsOrder=new DdsOrder();
        ddsOrder.setCreateTime(dateTime);
        ddsOrder.setNo(NO);
        ddsOrder.setStatus("0");
        ddsOrder.setUserId(userId);
        ddsOrderMapper.insert(ddsOrder);
        for (Integer id : ids) {
            DdsCart ddsCart = ddsCartMapper.selectById(id);
            DdsGoods ddsGoods = ddsGoodsMapper.selectById(ddsCart.getGoodsId());
            DdsOrderDetail ddsOrderDetail=new DdsOrderDetail();
            ddsOrderDetail.setOrderNo(ddsOrder.getNo());
            ddsOrderDetail.setOrderId(ddsOrder.getId());
            ddsOrderDetail.setSubtotal(ddsCart.getSubtotal());
            ddsOrderDetail.setGoodsId(ddsGoods.getId());
            ddsOrderDetail.setGoodsUnit(ddsGoods.getUnit());
            ddsOrderDetail.setGoodsPrice(ddsGoods.getPrice());
            ddsOrderDetail.setGoodsName(ddsGoods.getName());
            ddsOrderDetail.setPic(ddsGoods.getPic());
            ddsOrderDetail.setGoodsCount(ddsCart.getCount());
            ddsOrderDetailMapper.insert(ddsOrderDetail);
            ddsCartMapper.deleteById(id);

        }
        return ddsOrder.getId();
    }

    @Override
    public int getByUserId(Integer id) {
        QueryWrapper<DdsCart> queryWrapper=new QueryWrapper<DdsCart>()
                .eq("user_id",id);
        return ddsCartMapper.selectList(queryWrapper).size();
    }
}

